"""技能模板库"""

from typing import Dict, List, Any, Optional
from enum import Enum
from pydantic import BaseModel, Field
from .skill import SkillType, TargetType

class SkillCategory(Enum):
    """技能分类"""
    DAMAGE = "damage"         # 伤害技能
    HEAL = "heal"             # 治疗技能
    BUFF = "buff"             # 增益技能
    DEBUFF = "debuff"         # 减益技能
    PASSIVE = "passive"       # 被动技能
    UTILITY = "utility"       # 功能技能
    SUMMON = "summon"         # 召唤技能
    CONTROL = "control"       # 控制技能

class DamageType(Enum):
    """伤害类型"""
    PHYSICAL = "physical"     # 物理伤害
    MAGICAL = "magical"       # 魔法伤害
    TRUE = "true"             # 真实伤害
    HOLY = "holy"             # 神圣伤害
    DARK = "dark"             # 黑暗伤害
    FIRE = "fire"             # 火焰伤害
    ICE = "ice"               # 冰霜伤害
    LIGHTNING = "lightning"   # 雷电伤害
    POISON = "poison"         # 毒素伤害

class SkillTemplate(BaseModel):
    """技能模板"""
    template_id: str = Field(..., description="技能模板ID")
    name: str = Field(..., description="技能名称")
    description: str = Field("", description="技能描述")
    
    # 技能分类
    category: SkillCategory = Field(..., description="技能分类")
    skill_type: SkillType = Field(..., description="技能类型")
    target_type: TargetType = Field(..., description="目标类型")
    damage_type: Optional[DamageType] = Field(None, description="伤害类型")
    
    # 技能范围
    range_size: int = Field(0, description="技能范围(0:单体, 1-3:范围格数)")
    max_distance: int = Field(3, description="最大释放距离")
    
    # 技能数值
    base_value: int = Field(0, description="技能固定值")
    percentage_value: int = Field(0, description="技能百分比值(基于攻击力)")
    
    # 技能消耗和冷却
    cooldown: int = Field(0, description="技能冷却回合数")
    mp_cost: int = Field(0, description="魔法消耗(预留)")
    
    # 技能效果
    effects: List[Dict[str, Any]] = Field(default_factory=list, description="技能效果列表")
    
    # 技能标签
    tags: List[str] = Field(default_factory=list, description="技能标签")
    
    # 学习要求
    required_level: int = Field(1, description="学习所需等级")
    required_class: Optional[str] = Field(None, description="限定职业")
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典"""
        return {
            "template_id": self.template_id,
            "name": self.name,
            "description": self.description,
            "category": self.category.value,
            "skill_type": self.skill_type.value,
            "target_type": self.target_type.value,
            "damage_type": self.damage_type.value if self.damage_type else None,
            "range_size": self.range_size,
            "max_distance": self.max_distance,
            "base_value": self.base_value,
            "percentage_value": self.percentage_value,
            "cooldown": self.cooldown,
            "mp_cost": self.mp_cost,
            "effects": self.effects,
            "tags": self.tags,
            "required_level": self.required_level,
            "required_class": self.required_class
        }
    
    def calculate_damage(self, caster_attack: int) -> int:
        """计算技能伤害"""
        percentage_damage = int(caster_attack * self.percentage_value / 100)
        return self.base_value + percentage_damage

class SkillTemplateLibrary:
    """技能模板库"""
    
    _templates: Dict[str, SkillTemplate] = {}
    
    @classmethod
    def initialize_default_templates(cls):
        """初始化默认技能模板"""
        default_templates = [
            # 基础攻击技能
            SkillTemplate(
                template_id="basic_attack",
                name="基础攻击",
                description="普通的物理攻击",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.PHYSICAL,
                range_size=0,
                max_distance=1,
                base_value=20,
                percentage_value=100,
                cooldown=0,
                tags=["基础", "物理", "近战"]
            ),
            
            # 战士技能
            SkillTemplate(
                template_id="power_strike",
                name="强力打击",
                description="威力强大的重击",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.PHYSICAL,
                range_size=0,
                max_distance=1,
                base_value=30,
                percentage_value=350,
                cooldown=2,
                tags=["战士", "物理", "强化"]
            ),
            SkillTemplate(
                template_id="berserker_rage",
                name="狂暴",
                description="进入狂暴状态，提升攻击力",
                category=SkillCategory.BUFF,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.PHYSICAL,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=5,
                effects=[
                    {"type": "buff", "attribute": "attack", "value": 20, "duration": 3}
                ],
                tags=["狂战士", "增益", "自身"]
            ),
            SkillTemplate(
                template_id="blood_strike",
                name="嗜血打击",
                description="攻击敌人并回复自身生命值",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.PHYSICAL,
                range_size=0,
                max_distance=1,
                base_value=5,
                percentage_value=120,
                cooldown=3,
                effects=[
                    {"type": "heal_self", "value": 50, "percentage": 30}
                ],
                tags=["狂战士", "物理", "吸血"]
            ),
            
            # 坦克技能
            SkillTemplate(
                template_id="taunt",
                name="嘲讽",
                description="强制敌人攻击自己",
                category=SkillCategory.CONTROL,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=3,
                base_value=0,
                percentage_value=0,
                cooldown=1,
                effects=[
                    {"type": "taunt", "duration": 2}
                ],
                tags=["坦克", "控制", "嘲讽"]
            ),
            SkillTemplate(
                template_id="shield_bash",
                name="盾击",
                description="用盾牌攻击敌人",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.PHYSICAL,
                range_size=0,
                max_distance=1,
                base_value=8,
                percentage_value=110,
                cooldown=1,
                tags=["坦克", "物理", "控制"]
            ),
            SkillTemplate(
                template_id="whirlwind",
                name="旋风斩",
                description="攻击周围所有敌人",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.AREA,
                damage_type=DamageType.PHYSICAL,
                range_size=1,
                max_distance=1,
                base_value=50,
                percentage_value=80,
                cooldown=3,
                tags=["狂战士", "物理", "范围"]
            ),
            SkillTemplate(
                template_id="shield_wall",
                name="盾墙",
                description="大幅提升防御力",
                category=SkillCategory.BUFF,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=4,
                effects=[
                    {"type": "buff", "attribute": "defense", "value": 15, "duration": 3}
                ],
                tags=["坦克", "防御", "增益"]
            ),
            
            # 法师技能
            SkillTemplate(
                template_id="fireball",
                name="火球术",
                description="发射火球攻击敌人",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.AREA,
                damage_type=DamageType.FIRE,
                range_size=1,
                max_distance=4,
                base_value=15,
                percentage_value=120,
                cooldown=1,
                tags=["法师", "火焰", "范围"]
            ),
            SkillTemplate(
                template_id="ice_shard",
                name="冰锥术",
                description="发射冰锥，有几率冻结敌人",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.ICE,
                range_size=0,
                max_distance=5,
                base_value=12,
                percentage_value=110,
                cooldown=1,
                effects=[
                    {"type": "debuff", "attribute": "speed", "value": -5, "duration": 2, "chance": 30}
                ],
                tags=["法师", "冰霜", "减速"]
            ),
            SkillTemplate(
                template_id="life_drain",
                name="生命汲取",
                description="吸取敌人生命值",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.DARK,
                range_size=0,
                max_distance=3,
                base_value=8,
                percentage_value=90,
                cooldown=2,
                effects=[
                    {"type": "heal_self", "value": 0, "percentage": 50}
                ],
                tags=["死灵法师", "黑暗", "吸血"]
            ),
            SkillTemplate(
                template_id="curse",
                name="诅咒",
                description="降低敌人的属性",
                category=SkillCategory.DEBUFF,
                skill_type=SkillType.DEBUFF,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.DARK,
                range_size=0,
                max_distance=4,
                base_value=0,
                percentage_value=0,
                cooldown=3,
                effects=[
                    {"type": "debuff", "attribute": "attack", "value": -10, "duration": 3},
                    {"type": "debuff", "attribute": "defense", "value": -5, "duration": 3}
                ],
                tags=["死灵法师", "黑暗", "减益"]
             ),
             SkillTemplate(
                template_id="lightning_bolt",
                name="闪电箭",
                description="快速的雷电攻击",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.LIGHTNING,
                range_size=0,
                max_distance=6,
                base_value=8,
                percentage_value=130,
                cooldown=0,
                tags=["法师", "雷电", "快速"]
            ),
            
            # 死灵法师技能
            SkillTemplate(
                template_id="drain_life",
                name="生命汲取",
                description="吸取敌人生命值恢复自己",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.DARK,
                range_size=0,
                max_distance=4,
                base_value=8,
                percentage_value=100,
                cooldown=2,
                effects=[
                    {"type": "heal_self", "percentage": 80}
                ],
                tags=["死灵", "黑暗", "吸血"]
            ),
            SkillTemplate(
                template_id="summon_skeleton",
                name="召唤骷髅",
                description="召唤骷髅战士协助战斗",
                category=SkillCategory.SUMMON,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=2,
                base_value=0,
                percentage_value=0,
                cooldown=6,
                effects=[
                    {"type": "summon", "unit": "skeleton", "duration": 5}
                ],
                tags=["死灵", "召唤", "仆从"]
            ),
            
            # 刺客技能
            SkillTemplate(
                template_id="backstab",
                name="背刺",
                description="从背后攻击，必定暴击",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.PHYSICAL,
                range_size=0,
                max_distance=1,
                base_value=5,
                percentage_value=140,
                cooldown=3,
                effects=[
                    {"type": "guaranteed_crit"}
                ],
                tags=["刺客", "物理", "暴击"]
            ),
            SkillTemplate(
                template_id="poison_blade",
                name="毒刃",
                description="带毒的攻击",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.POISON,
                range_size=0,
                max_distance=1,
                base_value=6,
                percentage_value=100,
                cooldown=2,
                effects=[
                    {"type": "debuff", "attribute": "hp", "value": -5, "duration": 3}
                ],
                tags=["刺客", "毒素", "持续伤害"]
            ),
            SkillTemplate(
                template_id="stealth",
                name="潜行",
                description="进入隐身状态",
                category=SkillCategory.BUFF,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=5,
                effects=[
                    {"type": "stealth", "duration": 2}
                ],
                tags=["刺客", "隐身", "战术"]
            ),
            SkillTemplate(
                template_id="shadow_step",
                name="暗影步",
                description="瞬间移动到目标身后",
                category=SkillCategory.UTILITY,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=4,
                base_value=0,
                percentage_value=0,
                cooldown=4,
                effects=[
                    {"type": "teleport", "position": "behind_target"}
                ],
                tags=["刺客", "移动", "瞬移"]
            ),
            
            # 弓箭手技能
            SkillTemplate(
                template_id="precise_shot",
                name="精准射击",
                description="必中的精准一击",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.PHYSICAL,
                range_size=0,
                max_distance=6,
                base_value=8,
                percentage_value=130,
                cooldown=2,
                effects=[
                    {"type": "guaranteed_hit"}
                ],
                tags=["弓箭手", "物理", "精准"]
            ),
            SkillTemplate(
                template_id="multi_shot",
                name="多重射击",
                description="同时射击多个目标",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.AREA,
                damage_type=DamageType.PHYSICAL,
                range_size=2,
                max_distance=5,
                base_value=5,
                percentage_value=80,
                cooldown=3,
                tags=["弓箭手", "物理", "多目标"]
            ),
            SkillTemplate(
                template_id="piercing_arrow",
                name="穿透箭",
                description="穿透敌人防御的箭矢",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.PHYSICAL,
                range_size=0,
                max_distance=5,
                base_value=12,
                percentage_value=120,
                cooldown=2,
                effects=[
                    {"type": "ignore_defense", "percentage": 50}
                ],
                tags=["弓箭手", "物理", "穿透"]
            ),
            
            # 治疗技能
            SkillTemplate(
                template_id="heal",
                name="治疗术",
                description="恢复目标生命值",
                category=SkillCategory.HEAL,
                skill_type=SkillType.HEAL,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.HOLY,
                range_size=0,
                max_distance=4,
                base_value=25,
                percentage_value=80,
                cooldown=0,
                tags=["治疗", "神圣", "恢复"]
            ),
            SkillTemplate(
                template_id="group_heal",
                name="群体治疗",
                description="治疗范围内的所有友军",
                category=SkillCategory.HEAL,
                skill_type=SkillType.HEAL,
                target_type=TargetType.AREA,
                damage_type=DamageType.HOLY,
                range_size=2,
                max_distance=3,
                base_value=15,
                percentage_value=60,
                cooldown=3,
                tags=["治疗", "神圣", "群体"]
            ),
            SkillTemplate(
                template_id="blessing",
                name="祝福",
                description="提升友军的各项属性",
                category=SkillCategory.BUFF,
                skill_type=SkillType.HEAL,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=4,
                base_value=0,
                percentage_value=0,
                cooldown=5,
                effects=[
                    {"type": "buff", "attribute": "attack", "value": 10, "duration": 4},
                    {"type": "buff", "attribute": "defense", "value": 10, "duration": 4}
                ],
                tags=["治疗", "增益", "神圣"]
            ),
            
            # 圣骑士技能
            SkillTemplate(
                template_id="divine_strike",
                name="神圣打击",
                description="带有神圣力量的攻击",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.HOLY,
                range_size=0,
                max_distance=1,
                base_value=12,
                percentage_value=120,
                cooldown=2,
                tags=["圣骑士", "神圣", "物理"]
            ),
            SkillTemplate(
                template_id="holy_strike",
                name="神圣打击",
                description="带有神圣力量的攻击",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.HOLY,
                range_size=0,
                max_distance=1,
                base_value=12,
                percentage_value=120,
                cooldown=2,
                tags=["圣骑士", "神圣", "物理"]
            ),
            SkillTemplate(
                template_id="divine_protection",
                name="神圣护盾",
                description="为友军提供伤害减免",
                category=SkillCategory.BUFF,
                skill_type=SkillType.HEAL,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=3,
                base_value=0,
                percentage_value=0,
                cooldown=4,
                effects=[
                    {"type": "shield", "value": 30, "duration": 3}
                ],
                tags=["圣骑士", "防护", "神圣"]
            ),
            
            # 德鲁伊技能
            SkillTemplate(
                template_id="nature_heal",
                name="自然治疗",
                description="持续恢复生命值",
                category=SkillCategory.HEAL,
                skill_type=SkillType.HEAL,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=4,
                base_value=10,
                percentage_value=50,
                cooldown=1,
                effects=[
                    {"type": "heal_over_time", "value": 8, "duration": 3}
                ],
                tags=["德鲁伊", "自然", "持续"]
            ),
            SkillTemplate(
                template_id="thorn_strike",
                name="荆棘打击",
                description="自然魔法攻击",
                category=SkillCategory.DAMAGE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.MAGICAL,
                range_size=0,
                max_distance=3,
                base_value=10,
                percentage_value=110,
                cooldown=1,
                effects=[
                    {"type": "debuff", "attribute": "speed", "value": -3, "duration": 2, "chance": 40}
                ],
                tags=["德鲁伊", "自然", "减速"]
            ),
            SkillTemplate(
                template_id="wild_growth",
                name="野性成长",
                description="提升友军生命值上限",
                category=SkillCategory.BUFF,
                skill_type=SkillType.HEAL,
                target_type=TargetType.AREA,
                damage_type=None,
                range_size=2,
                max_distance=2,
                base_value=0,
                percentage_value=0,
                cooldown=6,
                effects=[
                    {"type": "buff", "attribute": "max_hp", "value": 20, "duration": 5}
                ],
                tags=["德鲁伊", "自然", "增益"]
            ),
            SkillTemplate(
                template_id="nature_blessing",
                name="自然祝福",
                description="为友军提供全属性增益",
                category=SkillCategory.BUFF,
                skill_type=SkillType.BUFF,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=3,
                base_value=0,
                percentage_value=0,
                cooldown=4,
                effects=[
                    {"type": "buff", "attribute": "attack", "value": 5, "duration": 4},
                    {"type": "buff", "attribute": "defense", "value": 5, "duration": 4},
                    {"type": "buff", "attribute": "speed", "value": 3, "duration": 4}
                ],
                tags=["德鲁伊", "自然", "增益"]
            ),
            
            # 被动技能
            SkillTemplate(
                template_id="iron_skin",
                name="钢铁皮肤",
                description="永久提升防御力",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "defense", "value": 8}
                ],
                tags=["被动", "防御", "永久"]
            ),
            SkillTemplate(
                template_id="battle_fury",
                name="战斗狂热",
                description="永久提升攻击力",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "attack", "value": 6}
                ],
                tags=["被动", "攻击", "永久"]
            ),
            SkillTemplate(
                template_id="swift_reflexes",
                name="敏捷反射",
                description="永久提升闪避率和速度",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "dodge_rate", "value": 10},
                    {"type": "passive_buff", "attribute": "speed", "value": 3}
                ],
                tags=["被动", "闪避", "速度"]
            ),
            SkillTemplate(
                template_id="keen_eye",
                name="鹰眼",
                description="永久提升命中率和暴击率",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "hit_rate", "value": 12},
                    {"type": "passive_buff", "attribute": "crit_rate", "value": 8}
                ],
                tags=["被动", "命中", "暴击"]
            ),
            SkillTemplate(
                template_id="steel_skin",
                name="钢铁皮肤",
                description="永久提升防御力",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "defense", "value": 8}
                ],
                tags=["被动", "防御", "永久"]
            ),
            SkillTemplate(
                template_id="battle_frenzy",
                name="战斗狂热",
                description="永久提升攻击力",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "attack", "value": 6}
                ],
                tags=["被动", "攻击", "永久"]
            ),
            SkillTemplate(
                template_id="regeneration",
                name="再生",
                description="每回合恢复少量生命值",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_heal", "value": 5}
                ],
                tags=["被动", "治疗", "持续"]
            ),
            SkillTemplate(
                template_id="vitality",
                name="生命力",
                description="永久提升生命值上限",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "max_hp", "value": 25}
                ],
                tags=["被动", "生命", "永久"]
            ),
            SkillTemplate(
                template_id="mana_shield",
                name="魔法护盾",
                description="战斗开始时获得护盾",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "initial_shield", "value": 20}
                ],
                tags=["被动", "护盾", "初始"]
            ),
            SkillTemplate(
                template_id="magic_shield",
                name="魔法护盾",
                description="永久提升魔法防御",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "magic_defense", "value": 10}
                ],
                tags=["被动", "魔防", "永久"]
            ),
            SkillTemplate(
                template_id="thorn_counter",
                name="荆棘反击",
                description="受到攻击时反弹伤害",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "reflect_damage", "percentage": 30}
                ],
                tags=["被动", "反击", "伤害"]
            ),
            SkillTemplate(
                template_id="agile_reflex",
                name="敏捷反射",
                description="永久提升闪避率",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "dodge_rate", "value": 15}
                ],
                tags=["被动", "闪避", "永久"]
            ),
            SkillTemplate(
                template_id="eagle_eye",
                name="鹰眼",
                description="永久提升命中率和暴击率",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "hit_rate", "value": 12},
                    {"type": "passive_buff", "attribute": "crit_rate", "value": 8}
                ],
                tags=["被动", "命中", "暴击"]
            ),
            SkillTemplate(
                template_id="lucky_strike",
                name="幸运打击",
                description="永久提升暴击率",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "crit_rate", "value": 15}
                ],
                tags=["被动", "暴击", "永久"]
            ),
            SkillTemplate(
                template_id="berserker_soul",
                name="狂战士之魂",
                description="生命值越低攻击力越高",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "berserker_rage", "max_bonus": 20}
                ],
                tags=["被动", "狂暴", "攻击"]
            ),
            SkillTemplate(
                template_id="swift_speed",
                name="迅捷",
                description="永久提升速度",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.PASSIVE,
                target_type=TargetType.SELF,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "passive_buff", "attribute": "speed", "value": 5}
                ],
                tags=["被动", "速度", "永久"]
            ),
            SkillTemplate(
                template_id="thorns",
                name="荆棘反击",
                description="受到攻击时反弹伤害",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=DamageType.MAGICAL,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "reflect_damage", "percentage": 30}
                ],
                tags=["被动", "反击", "荆棘"]
            ),
            SkillTemplate(
                template_id="berserker_spirit",
                name="狂战士之魂",
                description="生命值越低攻击力越高",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "conditional_buff", "condition": "low_hp", "attribute": "attack", "max_value": 15}
                ],
                tags=["被动", "条件", "狂战士"]
            ),
            SkillTemplate(
                template_id="lucky_strike",
                name="幸运打击",
                description="攻击时有几率触发额外效果",
                category=SkillCategory.PASSIVE,
                skill_type=SkillType.DAMAGE,
                target_type=TargetType.SINGLE,
                damage_type=None,
                range_size=0,
                max_distance=0,
                base_value=0,
                percentage_value=0,
                cooldown=0,
                effects=[
                    {"type": "trigger_chance", "chance": 15, "effect": "double_damage"}
                ],
                tags=["被动", "几率", "幸运"]
            )
        ]
        
        for template in default_templates:
            cls._templates[template.template_id] = template
    
    @classmethod
    def get_template(cls, template_id: str) -> SkillTemplate:
        """获取技能模板"""
        if not cls._templates:
            cls.initialize_default_templates()
        
        if template_id not in cls._templates:
            raise ValueError(f"技能模板 '{template_id}' 不存在")
        
        return cls._templates[template_id]
    
    @classmethod
    def get_all_templates(cls) -> Dict[str, SkillTemplate]:
        """获取所有技能模板"""
        if not cls._templates:
            cls.initialize_default_templates()
        
        return cls._templates.copy()
    
    @classmethod
    def get_templates_by_category(cls, category: SkillCategory) -> List[SkillTemplate]:
        """根据分类获取技能模板"""
        if not cls._templates:
            cls.initialize_default_templates()
        
        return [template for template in cls._templates.values() 
                if template.category == category]
    
    @classmethod
    def get_templates_by_class(cls, character_class: str) -> List[SkillTemplate]:
        """根据职业获取技能模板"""
        if not cls._templates:
            cls.initialize_default_templates()
        
        return [template for template in cls._templates.values() 
                if template.required_class is None or template.required_class == character_class]
    
    @classmethod
    def get_templates_by_tag(cls, tag: str) -> List[SkillTemplate]:
        """根据标签获取技能模板"""
        if not cls._templates:
            cls.initialize_default_templates()
        
        return [template for template in cls._templates.values() 
                if tag in template.tags]
    
    @classmethod
    def add_template(cls, template: SkillTemplate):
        """添加新技能模板"""
        cls._templates[template.template_id] = template
    
    @classmethod
    def remove_template(cls, template_id: str):
        """移除技能模板"""
        if template_id in cls._templates:
            del cls._templates[template_id]
    
    @classmethod
    def create_skill_from_template(cls, template_id: str, skill_id: str):
        """从模板创建技能"""
        from .skill import Skill
        
        template = cls.get_template(template_id)
        
        return Skill(
            id=skill_id,
            name=template.name,
            skill_type=template.skill_type,
            target_type=template.target_type,
            base_value=template.base_value,
            percentage_value=template.percentage_value,
            range_size=template.range_size,
            max_distance=template.max_distance,
            cost=template.mp_cost,
            cooldown=template.cooldown,
            effects=template.effects,
            description=template.description
        )

# 初始化默认技能模板
SkillTemplateLibrary.initialize_default_templates()